﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdNewFile.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "IAction.h"
#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "IDocument.h"
#include "IDocumentManager.h"
#include "IModelView.h"
#include "EventHandler/DocumentInitializer.h"
#include "IMainWindow.h"
#include "IUiView.h"
#include "IUserTransaction.h"
#include "PropertyPalette.h"
#include "ModelViewDlgUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace Sample;
using namespace gcmp;

REGISTER_COMMAND(CmdNewFile)

CmdNewFile::CmdNewFile()
    :CommandBase(ID_CMD_NEW_FILE)
{

}


gcmp::OwnerPtr<gcmp::IAction> CmdNewFile::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    //创建模型文档
    IDocumentManager *pDocumentManager = IDocumentManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocumentManager, L"获取文档管理器失败", L"GDMP", L"2023-09-30");
    //文档初始化
    DocumentInitializer documentInitializer;
    IDocument *pDocument = pDocumentManager->CreateDocument(DocumentType::ProjectDocument,L"", &documentInitializer);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"创建文档失败", L"GDMP", L"2023-09-30");
    OwnerPtr<IUserTransaction> opUserTransaction = IUserTransaction::Create(pDocument, L"新建文档");
    {
        IMainWindow *pMainWindow = IMainWindow::GetMainWindow();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pMainWindow, L"主窗口获取失败", L"GDMP", L"2019-5-16");
        //创建ui文档
        IUiDocumentViewManager *pDocumentViewManager = IUiDocumentViewManager::Get();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocumentViewManager, L"文档视图管理器获取失败", L"GDMP", L"2023-09-30");
        IUiDocument* pUiDocument = pDocumentViewManager->CreateUiDocument(pDocument);
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocument, L"UI文档创建失败", L"GDMP", L"2023-09-30");

        IModelView *pModelView = nullptr;
        auto modelViews = IModelView::GetAllModelViews(pDocument);

        FOR_EACH(curVeiw, modelViews)
        {
            if (curVeiw->GetViewType() == BuiltInViewType::ThreeDimensional)
            {
                pModelView = curVeiw;
                break;
            }
        }
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空", L"GDMP", L"2023-09-30");

        opUserTransaction->CommitAndResetHistory();

        IUiView* pUiView = pMainWindow->CreateOrOpenUiView(pUiDocument->GetId(), pModelView->GetElementId(), true, L"");
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiView, L"UI视图创建失败", L"GDMP", L"2023-09-30");
    }

    return nullptr;
}